package com.fg.cloud.framework.redis.finals;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.apache.shiro.cache.AbstractCacheManager;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.cache.support.SimpleValueWrapper;

import redis.clients.jedis.Jedis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Description:全局使用cacheManager
 * @author around
 * @date 2017年9月21日下午7:11:47
 */
public class DefaultCacheManager extends AbstractCacheManager {

	private static final Logger log = LoggerFactory.getLogger(DefaultCacheManager.class);  
	
	private org.springframework.cache.CacheManager cacheManager;
	
	/**
     * 设置spring cache manager
     * @param cacheManager
     */
    public void setCacheManager(org.springframework.cache.CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }
	
	@Override
	protected Cache<Object, Object> createCache(String name) throws CacheException {
		if (log.isTraceEnabled()) {  
            log.trace("create a cache name : " + name);  
        }
		org.springframework.cache.Cache springCache = cacheManager.getCache(name);
		return new SpringCacheWrapper(springCache);
		//return new ShiroMemcachedCache<Object, Object>(name);  
	}

	
	static class SpringCacheWrapper implements Cache {
        private org.springframework.cache.Cache springCache;

        SpringCacheWrapper(org.springframework.cache.Cache springCache) {
            this.springCache = springCache;
        }

        @Override
        public Object get(Object key) throws CacheException {
            Object value = springCache.get(key);
            if (value instanceof SimpleValueWrapper) {
                return ((SimpleValueWrapper) value).get();
            }
            return value;
        }

        @Override
        public Object put(Object key, Object value) throws CacheException {
            springCache.put(key, value);
            return value;
        }

        @Override
        public Object remove(Object key) throws CacheException {
            springCache.evict(key);
            return null;
        }

        @Override
        public void clear() throws CacheException {
            springCache.clear();
        }

        @Override
        public int size() {
            if(springCache.getNativeCache() instanceof Jedis) {
            	Jedis jedis = (Jedis) springCache.getNativeCache();
                return Integer.valueOf(jedis.getDB().toString());
            }
            throw new UnsupportedOperationException("invoke spring cache abstract size method not supported");
        }

        @Override
        public Set keys() {
            if(springCache.getNativeCache() instanceof Jedis) {
            	Jedis jedis = (Jedis) springCache.getNativeCache();
                return jedis.keys("*");
            }
            throw new UnsupportedOperationException("invoke spring cache abstract keys method not supported");
        }

        @Override
        public Collection values() {
            if(springCache.getNativeCache() instanceof Jedis) {
            	Jedis jedis = (Jedis) springCache.getNativeCache();
            	Set set = jedis.keys("*");
    
                List keys = new ArrayList();
                keys.addAll(set);
                if (!CollectionUtils.isEmpty(keys)) {
                    List values = new ArrayList(keys.size());
                    for (Object key : keys) {
                        Object value = get(key);
                        if (value != null) {
                            values.add(value);
                        }
                    }
                    return Collections.unmodifiableList(values);
                } else {
                    return Collections.emptyList();
                }
            }
            throw new UnsupportedOperationException("invoke spring cache abstract values method not supported");
        }
    }
}
